package com.cssl.config;

import com.cssl.service.DtsorderService;
import com.cssl.util.StorageVariable;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.lang.model.SourceVersion;
import java.util.LinkedList;
import java.util.List;

/**
 * 监控 优惠券,订单支付 过期时间
 */
@Component
public class CouponExpirationListener extends KeyExpirationEventMessageListener {

    //订单 或是 优惠券 同时过期
    private static List<String> orderKeyList = new LinkedList<String>();
    @Resource
    private DtsorderService dtsorderService;

    public CouponExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        final String expiredKey = message.toString();
        //expiredKey 键名
        String orderKey = "";
        if (expiredKey.contains(StorageVariable.ORDERPAYTIME)) {
            String[] split = expiredKey.split(":");
            orderKey = split[2];
            System.out.println("fsdafjksajkl"+orderKey);
        }
        //执行过期操作 订单: order:paytime:订单号
        orderKeyList.add(orderKey);
        orderKeyList.stream().forEach(s -> {
            //订单过期
            System.out.println("s = " + s);
            dtsorderService.updateOrderStatus(s);
        });

    }
}
